package com.nineclock.auth.config;

import com.nineclock.auth.exception.NcWebResponseExceptionTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import javax.sql.DataSource;

/**
 * 认证服务器
 *
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    DataSource dataSource;

    @Bean
    public ClientDetailsService  clientDetailsService(){
        return new JdbcClientDetailsService(dataSource);
    }

    //配置客户端的详情信息(客户端在发起认证时， 信息必须匹配才能 继续认证账号和密码)
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//        clients.inMemory()
//                //配置客户端的标识
//                .withClient("nc_client_app")
//                .secret(passwordEncoder.encode("123456"))
//                .scopes("all")
//                .redirectUris("http://localhost")
//                .authorizedGrantTypes("authorization_code","password")
//                .and()
//                .withClient("nc_client_pc")
//                .secret(passwordEncoder.encode("123456"))
//                .scopes("all")
//                .redirectUris("http://localhost")
//                .authorizedGrantTypes("authorization_code","password");

        clients.withClientDetails(clientDetailsService());
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .tokenKeyAccess("permitAll")
                .checkTokenAccess("permitAll")
                .allowFormAuthenticationForClients();
    }
    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    ClientDetailsService clientDetailsService;

    @Autowired
    TokenStore tokenStore;


    @Autowired
    JwtAccessTokenConverter tokenConverter;

    @Bean
    public AuthorizationServerTokenServices tokenServices(){
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        //token的客户端详情信息
        tokenServices.setClientDetailsService(clientDetailsService);
        //token的存储对象
        tokenServices.setTokenStore(tokenStore);
        //token的有效期, 单位 秒
        tokenServices.setAccessTokenValiditySeconds(60 * 60 * 12);
        //token 刷新令牌的有效期 单位秒
        tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 30);

        //配置token增强
        tokenServices.setTokenEnhancer(tokenConverter);

        return tokenServices;
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .exceptionTranslator(new NcWebResponseExceptionTranslator())
                .tokenServices(tokenServices())
                .authenticationManager(authenticationManager)
                .allowedTokenEndpointRequestMethods(HttpMethod.POST);
    }
}